/**
 *
 */
package com.oniontech.stamptour.fragment.app;

import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.oniontech.stamptour.MainActivity;
import com.oniontech.stamptour.R;
import com.oniontech.stamptour.common.Task.ContentsListQuery;
import com.oniontech.stamptour.common.adapter.Bean.ContentsListBean;
import com.oniontech.stamptour.common.adapter.StampContListAdapter;
import com.oniontech.stamptour.fragment.DefaultFragment;
import com.oniontech.stamptour.fragment.listener.FragmentListener;
import com.oniontech.stamptour.fragment.listener.QueryListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kty
 */
public class StampMainToListFragment extends DefaultFragment implements FragmentListener{
    private String themeCode = null;
    private String themeTitle = null;
    private StampContListAdapter adpater = null;
    private ListView listview = null;
    private TextView navTitle_txt = null;
    private View view = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_to_list, null);
        navTitle_txt = (TextView) view.findViewById(R.id.navTitle_txt);
        listview = (ListView) view.findViewById(R.id.listview);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ContentsListBean data = (ContentsListBean) adapterView.getItemAtPosition(i);
                Fragment fragment = new WebViewFragmentContDetail();
                ((WebViewFragmentContDetail) fragment).onFragmentData(data);
                ((MainActivity) getActivity()).setFragment(fragment, MainActivity.FRAGMENT_ADD);
            }
        });
        new ContentsListQuery(themeCode, null, new QueryListener() {
            @Override
            public void queryComplete(Object data) {
                if(adpater==null) {
                    List<ContentsListBean> bean = (List<ContentsListBean>)data;
                    adpater = new StampContListAdapter(bean);
                    listview.setAdapter(adpater);

                    themeTitle = bean.get(0).getThemeName();
                    navTitle_txt.setText(themeTitle);
                }
            }
        }).execute();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(themeTitle != null)
            navTitle_txt.setText(themeTitle);
    }

    public void reflash() {
        new ContentsListQuery(themeCode,null, new QueryListener() {
            @Override
            public void queryComplete(Object data) {
                adpater.notifyDataSetChanged();
                adpater.notifyDataSetInvalidated();
            }
        }).execute();
    }



    /**
     * On fragment data.
     *
     * @param obj      the obj
     */
    @Override
    public void onFragmentData(Object obj) {
        if(obj instanceof  String) {
            themeCode = (String) obj;
        }
    }
}
